package cn.sciento.wechat.conf;

import java.nio.charset.StandardCharsets;
import cn.sciento.wechat.service.BaseWechatService;
import cn.sciento.wechat.service.WeChatMenuService;
import cn.sciento.wechat.service.WeChatMessageManageService;
import cn.sciento.wechat.service.WechatMaterialManageService;
import cn.sciento.wechat.service.impl.BaseWechatServiceImpl;
import cn.sciento.wechat.service.impl.WeChatMenuServiceImp;
import cn.sciento.wechat.service.impl.WeChatMessageManageServiceImp;
import cn.sciento.wechat.service.impl.WechatMaterialManageServiceImp;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

@Configuration
public class WechatConfig {
  @Bean
  @ConditionalOnMissingBean
  public BaseWechatService baseWechatService() {
    return (BaseWechatService)new BaseWechatServiceImpl();
  }
  
  @Bean
  @ConditionalOnMissingBean(name = {"wdRestTemplate"})
  public RestTemplate wdRestTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
    return restTemplate;
  }
  
  @Bean
  @ConditionalOnMissingBean(name = {"wdRedisTemplate"})
  public RedisTemplate wdRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate();
    redisTemplate.setConnectionFactory(redisConnectionFactory);
    redisTemplate.setKeySerializer((RedisSerializer)new StringRedisSerializer());
    redisTemplate.setValueSerializer((RedisSerializer)new GenericJackson2JsonRedisSerializer());
    return redisTemplate;
  }
  
  @Bean
  @ConditionalOnMissingBean
  public WechatMaterialManageService wechatMaterialManageService() {
    return (WechatMaterialManageService)new WechatMaterialManageServiceImp();
  }
  
  @Bean
  @ConditionalOnMissingBean
  public WeChatMenuService weChatMenuService() {
    return (WeChatMenuService)new WeChatMenuServiceImp();
  }
  
  @Bean
  @ConditionalOnMissingBean
  public WeChatMessageManageService weChatMessageManageService() {
    return (WeChatMessageManageService)new WeChatMessageManageServiceImp();
  }
}
